package cn.sumpu.app.guoguojie.biz.impl.task;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.biz.Task;
import cn.sumpu.app.guoguojie.util.CmsUtil;

public class TaskImpl implements Task {

	private final static Logger logger = LoggerFactory
			.getLogger(TaskImpl.class);

	private final static DateFormat df = new SimpleDateFormat("yyyyMMdd");

	@Override
	public void publishContent() {
		try {
			String cmsPath = CmsUtil.getFullPathRelateClass("../../cms",
					TaskImpl.class);
			String dateStr = df.format(new Date());
			// 生成最终发布的jsp文件
			File defaultTemplateFile = new File(cmsPath + File.separator
					+ "square.html");
			File latestTemplateFile = new File(cmsPath + File.separator
					+ "square_" + dateStr + ".html");
			String resourcePath = cmsPath + File.separator;
			String template = null;
			if (latestTemplateFile.exists()) {
				template = "square_" + dateStr + ".html";
			} else if (defaultTemplateFile.exists()) {
				template = "square.html";
			}
			if (null != template) {
				Map<String, String> templateParams = new HashMap<String, String>();
				String destfile = cmsPath + File.separator + "square.jsp";
				String charset = "UTF-8";
				CmsUtil.generate(resourcePath, template, templateParams,
						destfile, charset);
			} else {
				if (logger.isErrorEnabled()) {
					logger.error("Publish content failed,Becase template file not found.");
				}
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Publish content failed. ", e);
			}
		}
	}

}
